package com.lv.suyiyong.hfr.ui

import android.view.KeyEvent
import android.view.View
import androidx.core.content.ContentProviderCompat.requireContext
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.lv.common.base.BaseActivity
import com.lv.common.im.GenerateTestUserSig
import com.lv.common.im.push.OfflineMessageDispatcher
import com.lv.common.utils.ARoutUtils
import com.lv.common.utils.TimeUtil
import com.lv.common.utils.YgxUtilsKt
import com.lv.service.repo.UserDbHelper
import com.lv.suyiyong.hfr.R
import com.lv.suyiyong.hfr.databinding.ActivityLoginOutBinding
import com.tencent.imsdk.v2.V2TIMManager
import com.tencent.qcloud.tuicore.TUILogin
import com.tencent.qcloud.tuicore.interfaces.TUICallback
import kotlin.system.exitProcess

/**
 * 作者： ygx
 * 创建日期：2023/5/31 4:12 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：被挤下线
 */
@Route(path = ARoutUtils.LoginOutActivity)
class LoginOutActivity : BaseActivity<ActivityLoginOutBinding>(), View.OnClickListener {
    override fun getLayoutRes() = R.layout.activity_login_out
    override fun initView() {
        super.initView()
        initStatusBar(this, true, true)
        mBinding.apply {
            tvSure.setOnClickListener(this@LoginOutActivity)
            tvCancel.setOnClickListener(this@LoginOutActivity)
            ivClose.setOnClickListener(this@LoginOutActivity)
        }
    }


    override fun initConfig() {
        super.initConfig()
        OfflineMessageDispatcher.updateBadge(this@LoginOutActivity, 0)
        UserDbHelper.getLiveUserInfo(this).observeKt {
            TUILogin.login(
                this,
                GenerateTestUserSig.SDKAPPID,
                it?.txaccid,
                it?.txaccidpassword,
                object : TUICallback() {
                    override fun onError(code: Int, desc: String) {
//                        loadFinished()
                        LogUtils.e("onError")
                    }

                    override fun onSuccess() {
                        UserDbHelper.deleteUserInfo(this@LoginOutActivity)
                        TUILogin.logout(object : TUICallback() {
                            override fun onSuccess() {}
                            override fun onError(errorCode: Int, errorMessage: String) {}
                        })
                    }
                })
        }
//        UserDbHelper.deleteUserInfo(this)
        //清除Token
        YgxUtilsKt.setAppToken("", "", "")
        TUILogin.logout(object : TUICallback() {
            override fun onSuccess() {}
            override fun onError(errorCode: Int, errorMessage: String) {}
        })
        V2TIMManager.getConversationManager().removeConversationListener(null)

    }


    override fun initData() {
        super.initData()
        val time = TimeUtil.getTime()
        mBinding.tvTitle.text = "提示"
        mBinding.tvContent.text = "您的帐号$time 在其他设备登录，是否重新登录"

    }


    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.tv_sure -> {
                ActivityUtils.finishAllActivities()
                ARouter.getInstance().build(ARoutUtils.OneKeyLoginActivityURL).navigation()
            }
            R.id.tv_cancel, R.id.iv_close -> {
                exitPage()
            }


        }
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitPage()
            return false
        }
        return super.onKeyDown(keyCode, event)
    }

    private fun exitPage() {
        ActivityUtils.finishAllActivities()
        exitProcess(0)
    }


}